package com.youlai.boot.system.controller;

import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.system.service.LpsOrderDownloadService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.LpsOrderDownloadForm;
import com.youlai.boot.system.model.query.LpsOrderDownloadQuery;
import com.youlai.boot.system.model.vo.LpsOrderDownloadVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 下载记录前端控制层
 *
 * @author youlaitech
 * @since 2025-08-10 19:52
 */
@Tag(name = "下载记录接口")
@RestController
@RequestMapping("/api/v1/lpsOrderDownloads")
@RequiredArgsConstructor
public class LpsOrderDownloadController  {

    private final LpsOrderDownloadService lpsOrderDownloadService;

    @Operation(summary = "下载记录分页列表")
    @GetMapping("/page")
    //@PreAuthorize("@ss.hasPerm('system:lpsOrderDownload:query')")
    public PageResult<LpsOrderDownloadVO> getLpsOrderDownloadPage(LpsOrderDownloadQuery queryParams ) {
        Long userId = SecurityUtils.getUserId();
        queryParams.setUserId(userId);
        IPage<LpsOrderDownloadVO> result = lpsOrderDownloadService.getLpsOrderDownloadPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "下载记录分页列表")
    @GetMapping("/page1")
    //@PreAuthorize("@ss.hasPerm('system:lpsOrderDownload:query')")
    public PageResult<LpsOrderDownloadVO> getLpsOrderDownloadPage1(LpsOrderDownloadQuery queryParams ) {
        IPage<LpsOrderDownloadVO> result = lpsOrderDownloadService.getLpsOrderDownloadPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增下载记录")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:lpsOrderDownload:add')")
    public Result<Void> saveLpsOrderDownload(@RequestBody @Valid LpsOrderDownloadForm formData ) {
        boolean result = lpsOrderDownloadService.saveLpsOrderDownload(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取下载记录表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:lpsOrderDownload:edit')")
    public Result<LpsOrderDownloadForm> getLpsOrderDownloadForm(
        @Parameter(description = "下载记录ID") @PathVariable Long id
    ) {
        LpsOrderDownloadForm formData = lpsOrderDownloadService.getLpsOrderDownloadFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改下载记录")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:lpsOrderDownload:edit')")
    public Result<Void> updateLpsOrderDownload(
            @Parameter(description = "下载记录ID") @PathVariable Long id,
            @RequestBody @Validated LpsOrderDownloadForm formData
    ) {
        boolean result = lpsOrderDownloadService.updateLpsOrderDownload(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除下载记录")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:lpsOrderDownload:delete')")
    public Result<Void> deleteLpsOrderDownloads(
        @Parameter(description = "下载记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = lpsOrderDownloadService.deleteLpsOrderDownloads(ids);
        return Result.judge(result);
    }
}
